package com.agree.util.excel;

import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.agree.util.excel.config.Configuration;
import com.agree.util.excel.entity.TableInfo;
import com.agree.util.excel.enums.MappingType;
import com.agree.util.excel.enums.TablesEnum;
import com.agree.util.excel.handlers.db.DBHandler;
import com.agree.util.excel.handlers.db.OracleDBHandler;
import com.agree.util.excel.handlers.poi.DefaultPOIHandler;
import com.agree.util.excel.entity.InterfaceColMapEntity;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.util.List;

/**
 * Created by wangmu on 2017/12/1.
 */
public class GeneratorRunner {

    public static void main(String[] args) {

        long start = System.currentTimeMillis();
        Configuration configuration = new Configuration();

        //数据库设置

        configuration
                .dbConfig()
                    .driverClassName("oracle.jdbc.driver.OracleDriver")
                    .url("jdbc:oracle:thin:@192.9.200.122:1521:orcl")
                    .username("uccdev")
                    .password("uccdev")
                    .includTables(TablesEnum.WANGMU_CIS.getTableNames())
                    .commonPrefix(new String[]{"TB_UCC","TB_TEMP_"})
                .end()
                //数据库查询对象映射到Excel表实体类配置
                .mppingConfig()
                    .tableMappings()
                        .anyTables(TablesEnum.WANGMU_CIS.getTableNames(),MappingType.SIMPLE_QUERY)
                    .end()
                .end()
                //poi设置
                .poiConfig()
                    .excelType(ExcelType.XSSF)
                    .outPath("F:/out-excel")
                .end();


        DBHandler oracleDBHandler = new OracleDBHandler(configuration);
        List<TableInfo> allTableInfo = oracleDBHandler.getAllTableInfo();

        File file = new File(configuration.poiConfig().outPath());

        if(!file.exists()) {
            file.mkdirs();
        }

        allTableInfo.parallelStream()
                .forEach(tableInfo -> {
                    DefaultPOIHandler poiHandler = new DefaultPOIHandler(configuration);
                    Workbook workbook = poiHandler.createSheetExcel(configuration.poiConfig().excelType(), InterfaceColMapEntity.class, tableInfo);
                    poiHandler.writeExcel(workbook,new File(file,tableInfo.getComment() + configuration.poiConfig().fileExtension()));
                    System.out.println("输出====="+tableInfo.getComment()+configuration.poiConfig().fileExtension()+"文件====");
                });
        long end = System.currentTimeMillis();
        System.out.println("执行时间为: "+ (end-start) + " ms");

    }
}
